# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT, get_lang
from odoo.tools.float_utils import float_compare, float_round


class PurchaseOrderExt(models.Model):
    _inherit = 'purchase.order'

    subcontracting = fields.Boolean(string='Subcontracting', readonly=True)
    subcon_out = fields.Boolean(string='Shipped', readonly=True)
    subcon_out_date = fields.Datetime(string='Shipping Date', readonly=True)
    subcon_in = fields.Boolean(string='Received', readonly=True)
    subcon_in_date = fields.Datetime(string='Receiving Date', readonly=True)

    # def action_subcon_out(self):
    #     for rec in self:
    #         rec.subcon_out = True
    #         rec.subcon_out_date = fields.Datetime.now()

    # def action_subcon_in(self):
    #     for rec in self:
    #         rec.subcon_in = True
    #         rec.subcon_in_date = fields.Datetime.now()


class PurchaseOrderLineExt(models.Model):
    _inherit = 'purchase.order.line'

    workorder_id = fields.Many2one('mrp.workorder', string='Operation')
    default_code = fields.Char(string='Product Code', related='product_id.default_code')
    s_area = fields.Char(string='Area', copy=False, )

    @api.depends('product_qty', 'product_uom', 'company_id', 'product_id')
    def _compute_price_unit_and_date_planned_and_name(self):
        for line in self:
            if not line.product_id or line.invoice_lines or not line.company_id:
                continue

            line.name = line.product_id.default_code if line.product_id else ''

            params = {'order_id': line.order_id}
            seller = line.product_id._select_seller(
                partner_id=line.partner_id,
                quantity=line.product_qty,
                date=line.order_id.date_order and line.order_id.date_order.date() or fields.Date.context_today(line),
                uom_id=line.product_uom,
                params=params)

            if seller or not line.date_planned:
                line.date_planned = line._get_date_planned(seller).strftime(DEFAULT_SERVER_DATETIME_FORMAT)

            # If not seller, use the standard price. It needs a proper currency conversion.
            if not seller:
                unavailable_seller = line.product_id.seller_ids.filtered(
                    lambda s: s.partner_id == line.order_id.partner_id)
                if not unavailable_seller and line.price_unit and line.product_uom == line._origin.product_uom:
                    # Avoid to modify the price unit if there is no price list for this partner and
                    # the line has already one to avoid to override unit price set manually.
                    continue
                po_line_uom = line.product_uom or line.product_id.uom_po_id
                price_unit = line.env['account.tax']._fix_tax_included_price_company(
                    line.product_id.uom_id._compute_price(line.product_id.standard_price, po_line_uom),
                    line.product_id.supplier_taxes_id,
                    line.taxes_id,
                    line.company_id,
                )
                price_unit = line.product_id.cost_currency_id._convert(
                    price_unit,
                    line.currency_id,
                    line.company_id,
                    line.date_order or fields.Date.context_today(line),
                    False
                )
                line.price_unit = float_round(price_unit, precision_digits=max(line.currency_id.decimal_places, self.env['decimal.precision'].precision_get('Product Price')))
                continue

            price_unit = line.env['account.tax']._fix_tax_included_price_company(seller.price, line.product_id.supplier_taxes_id, line.taxes_id, line.company_id) if seller else 0.0
            price_unit = seller.currency_id._convert(price_unit, line.currency_id, line.company_id, line.date_order or fields.Date.context_today(line), False)
            price_unit = float_round(price_unit, precision_digits=max(line.currency_id.decimal_places, self.env['decimal.precision'].precision_get('Product Price')))
            line.price_unit = seller.product_uom._compute_price(price_unit, line.product_uom)
            line.discount = seller.discount or 0.0
